package com.jgssp.ad.sdk.mediation.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.bytedance.sdk.openadsdk.mediation.MediationConstant;
import com.bytedance.sdk.openadsdk.mediation.bridge.custom.native_ad.MediationCustomNativeAd;

import cn.jiguang.jgssp.ad.data.ADJgNativeAdInfo;
import cn.jiguang.jgssp.ad.data.ADJgNativeExpressAdInfo;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgNativeVideoListener;
import cn.jiguang.jgssp.util.ADJgViewUtil;


public class GmNativeExpressAd extends MediationCustomNativeAd {

    private ADJgNativeExpressAdInfo nativeExpressAdInfo;
    private int biddingType;
    private Context context;

    public ADJgNativeExpressAdInfo getNativeExpressAdInfo() {
        return nativeExpressAdInfo;
    }

    public GmNativeExpressAd(Context context, ADJgNativeExpressAdInfo nativeExpressAdInfo, int biddingType) {
        this.context = context;
        this.nativeExpressAdInfo = nativeExpressAdInfo;
        this.biddingType = biddingType;
        setExpressAd(true);
        nativeExpressAdInfo.setVideoListener(new ADJgNativeVideoListener() {

            @Override
            public void onVideoLoad(ADJgNativeAdInfo adJgNativeAdInfo) {

            }

            @Override
            public void onVideoStart(ADJgNativeAdInfo adJgNativeAdInfo) {
                callVideoStart();
            }

            @Override
            public void onVideoComplete(ADJgNativeAdInfo adJgNativeAdInfo) {
                callVideoCompleted();
            }

            @Override
            public void onVideoPause(ADJgNativeAdInfo adJgNativeAdInfo) {
                callVideoPause();
            }

            @Override
            public void onVideoError(ADJgNativeAdInfo adJgNativeAdInfo, ADJgError adJgError) {
                callVideoError(-1, "视频播放失败");
            }
        });
    }

    @Override
    public View getExpressView() {
        Log.i(Const.TAG, "getExpressView " + Thread.currentThread().toString());
        if (nativeExpressAdInfo != null && context != null) {
            FrameLayout viewGroup = new FrameLayout(context);
            return nativeExpressAdInfo.getNativeExpressAdView(viewGroup);
        }
        return null;
    }

    @Override
    public void render() {
        Log.i(Const.TAG, "render " + Thread.currentThread().toString());
        if (nativeExpressAdInfo != null && context != null) {
            callRenderSuccess(MediationConstant.FULL_WIDTH, MediationConstant.AUTO_HEIGHT);
            FrameLayout viewGroup = new FrameLayout(context);
            nativeExpressAdInfo.render(viewGroup);
        }
    }

    @Override
    public boolean hasDislike() {
        return true;
    }

    @Override
    public MediationConstant.AdIsReadyStatus isReadyCondition() {
        if (nativeExpressAdInfo != null && !nativeExpressAdInfo.isReleased()) {
            return MediationConstant.AdIsReadyStatus.AD_IS_READY;
        } else {
            return MediationConstant.AdIsReadyStatus.AD_IS_NOT_READY;
        }
    }

    /**
     * 是否clientBidding广告
     *
     * @return
     */
    public boolean isClientBidding() {
        return biddingType == MediationConstant.AD_TYPE_CLIENT_BIDING;
    }

    public void clear() {
        ADJgViewUtil.removeSelfFromParent(getExpressView());
    }
}
